using UnityEngine;

namespace UndergroundSurvival.Rooms
{
    public class WaterTreatmentRoom : BaseRoom
    {
        [Header("Water Treatment Settings")]
        [SerializeField] private float waterProductionRate = 1f;
        [SerializeField] private float powerConsumptionRate = 0.2f;
        [SerializeField] private float filterEfficiency = 1f;
        [SerializeField] private float filterDurability = 100f;
        [SerializeField] private float filterDegradationRate = 0.1f;

        private float currentFilterDurability;
        private float currentEfficiency;

        protected override void Start()
        {
            base.Start();
            currentFilterDurability = filterDurability;
            currentEfficiency = filterEfficiency;
        }

        protected override void Update()
        {
            base.Update();

            if (!isBuilt || !isFunctioning) return;

            // 检查电力供应
            if (ResourceManager.Instance != null)
            {
                float actualPowerConsumption = powerConsumptionRate * currentEfficiency;
                if (ResourceManager.Instance.ModifyResource(ResourceType.Power, -actualPowerConsumption * Time.deltaTime))
                {
                    // 成功消耗电力，生产净水
                    float waterProduced = waterProductionRate * currentEfficiency * Time.deltaTime;
                    ResourceManager.Instance.ModifyResource(ResourceType.Water, waterProduced);

                    // 过滤器耐久度降低
                    currentFilterDurability -= filterDegradationRate * Time.deltaTime;
                    currentEfficiency = filterEfficiency * (currentFilterDurability / filterDurability);

                    // 检查是否需要更换过滤器
                    if (currentFilterDurability <= 0)
                    {
                        isFunctioning = false;
                    }
                }
                else
                {
                    // 电力不足，停止运行
                    isFunctioning = false;
                }
            }
        }

        public void ReplaceFilter()
        {
            if (ResourceManager.Instance != null)
            {
                if (ResourceManager.Instance.ModifyResource(ResourceType.Metal, -1f))
                {
                    currentFilterDurability = filterDurability;
                    currentEfficiency = filterEfficiency;
                    isFunctioning = true;
                }
            }
        }

        public override void OnRoomUpgraded()
        {
            base.OnRoomUpgraded();
            waterProductionRate *= 1.5f;
            filterEfficiency *= 1.2f;
            currentEfficiency = filterEfficiency;
        }

        public float GetFilterDurability()
        {
            return currentFilterDurability / filterDurability;
        }

        public float GetCurrentEfficiency()
        {
            return currentEfficiency;
        }

        public bool NeedsFilterReplacement()
        {
            return currentFilterDurability <= filterDurability * 0.2f;
        }
    }
} 